package pub.developers.forum.infrastructure;

import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;
import pub.developers.forum.api.model.PageRequestModel;
import pub.developers.forum.common.enums.AuditStateEn;
import pub.developers.forum.domain.entity.BasePosts;
import pub.developers.forum.domain.repository.PostsRepository;
import pub.developers.forum.infrastructure.dal.dao.InformDAO;
import pub.developers.forum.infrastructure.dal.dao.PostsDAO;
import pub.developers.forum.infrastructure.dal.dataobject.PostsDO;
import pub.developers.forum.infrastructure.transfer.PostsTransfer;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Repository
public class PostsRepositoryImpl implements PostsRepository {

    @Resource
    private PostsDAO postsDAO;


    @Resource
    private InformDAO informDAO;


    @Override
    public BasePosts get(Long id) {
        return PostsTransfer.toBasePosts(postsDAO.get(id));
    }

    @Override
    public List<BasePosts> queryInIds(Set<Long> ids) {
        return PostsTransfer.toBasePostsList(postsDAO.queryInIds(ids));
    }

    @Override
    public List<Long> getAllIdByAuthorId(Long authorId) {
        return postsDAO.getAllIdByAuthorId(authorId, AuditStateEn.PASS.getValue());
    }

    @Override
    public void increaseComments(Long id, Date updateAt) {
        postsDAO.increaseComments(id, updateAt);
    }

    @Override
    public void decreaseComments(Long id, Date updateAt) {
        postsDAO.decreaseComments(id, updateAt);
    }

    @Override
    public void increaseViews(Long id, Date updateAt) {
        postsDAO.increaseViews(id, updateAt);
    }

    @Override
    public void increaseApproval(Long id, Date updateAt) {
        postsDAO.increaseApproval(id, updateAt);
    }

    @Override
    public void decreaseApproval(Long id, Date updateAt) {
        postsDAO.decreaseApproval(id, updateAt);
    }

    @Override
    public void delete(Long id) {
        postsDAO.delete(id);
    }

    @Override
    public void softDelete(Long id,Date update) {
        postsDAO.softDelete(id,update);
    }

    @Override
    public void update(BasePosts basePosts) {
        PostsDO postsDO = PostsDO.builder()
                .sort(basePosts.getSort())
                .top(basePosts.getTop())
                .marrow(basePosts.getMarrow())
                .official(basePosts.getOfficial())
                .build();
        if (!ObjectUtils.isEmpty(basePosts.getAuditState())) {
            postsDO.setAuditState(basePosts.getAuditState().getValue());
        }
        postsDO.setId(basePosts.getId());

        postsDAO.update(postsDO);
    }

    @Override
    public Long[] getPostsId(Long id) {
        return postsDAO.getPostsIdByUserid(id);
    }

    public Long[] getPostsId(Long userId,PageRequestModel pageRequestModel){
        return postsDAO.getPostsIdByUseridAndPage(userId,pageRequestModel);
    }

    @Override
    public Long[] getPostsId(PageRequestModel pageRequestModel) {
        return postsDAO.getPostsId(pageRequestModel);
    }

    @Override
    public Long[] getBanPostsId(PageRequestModel pageRequestModel) {
        return postsDAO.getBanPostsId(pageRequestModel);
    }

    public Long[] getReportedPostsId(PageRequestModel pageRequestModel) {
        return postsDAO.getReportedPostsId(pageRequestModel);
    }



    //根据帖子id 拿到对应的帖子标题
    @Override
    public BasePosts getPostInformation(Long postId) {
        PostsDO postsDO = postsDAO.selectPostById(postId);
        BasePosts result = new BasePosts();
        if (postsDO != null) {
            result.setId(postsDO.getId());
            result.setAuditState(AuditStateEn.valueOf(postsDO.getAuditState()));
            result.setOfficial(postsDO.getOfficial());
            result.setAuthorId(postsDO.getAuthorId());
//            result.setContentType(ContentTypeEn.valueOf(postsDO.getContentType()));
            result.setTop(postsDO.getTop());
            result.setMarrow(postsDO.getMarrow());
            result.setCreateAt(postsDO.getCreateAt());
            result.setTitle(postsDO.getTitle());
            result.setHeadImg(postsDO.getHeadImg());
            result.setMarkdownContent(postsDO.getMarkdownContent());
            result.setHtmlContent(postsDO.getHtmlContent());
            result.setViews(postsDO.getViews());
            //点赞量
            result.setApprovals(postsDO.getApprovals());
            //评论量
            result.setComments(postsDO.getComments());
            //根据帖子的类型id , 拿到帖子所属的类型名称
        }
        return result;
    }

    public  Long[] getFilterPosts(PageRequestModel pageRequestModel){
        return postsDAO.getFilterPosts(pageRequestModel);
    }

    //获取被过滤的帖子的总数
    public Long getFilterTotal(PageRequestModel pageRequestModel) {
        return postsDAO.getFilterTotal(pageRequestModel);
    }

    // 获取所有帖子
    @Override
    public List<BasePosts> getAllPost() {
        List<PostsDO> tmp = postsDAO.getAllPost();
        List<BasePosts> res = new ArrayList<>();
        for(PostsDO post : tmp){
            res.add(PostsTransfer.toBasePosts(post));
        }
        return res;
    }

    @Override
    public void deleteByAuthorId(Long authorId) {
        postsDAO.deleteByAuthorId(authorId);
    }


}
